#!/usr/bin/env python
# encoding: utf-8

"""
@author: Cao
@contact: caoqiang0012@163.com
@file: SingleRobot.py
@time: 01/07/2018 1:55 PM
"""
import paramiko
from Public import SimpleSSH
import time
import sys

class restartSingleRobot:
    def __init__(self, hostname, username, password, port=22):

        self.ssh = paramiko.SSHClient()
        self.simpleSSH = SimpleSSH.SimpleSSH()
        self.hostname = hostname
        print('hostname:' + self.hostname)

        self.username = username
        self.password = password
        self.port = port
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect(self.hostname, 22, self.username, self.password)

    def enableBash(self):

        self.ssh.exec_command("source /robots/machine-global-cache-for-personal-service/bashrc")

    def stopService(self):

        self.ssh.exec_command("source /robots/machine-global-cache-for-personal-service/bashrc\n/robots/machine-global-cache-for-personal-service/usr/local/services/tools/bin/robot-control stop")

    def startService(self):

        self.ssh.exec_command("source /robots/machine-global-cache-for-personal-service/bashrc\n/robots/machine-global-cache-for-personal-service/usr/local/services/tools/bin/robot-control start")

    def robotState(self):

        stdin, stdout, stderr = self.ssh.exec_command(
            "source /robots/machine-global-cache-for-personal-service/bashrc\n/robots/machine-global-cache-for-personal-service/usr/local/services/tools/bin/robot-control status")

        lines = stdout.readlines()
        # print(lines)
        if '====' in lines[7]:
            x = 7
        else:
            x = 8

        # print('x = ' + str(x))
        time.sleep(2)

        dict = {}

        for i in range(1, x):
            line = lines[i].strip('\n').split(": ")
            # print(line)

            dict[line[0]] = line[1]

        state = dict['State']
        return state

    def close(self):
        self.ssh.close()

if __name__ == '__main__':

    #robotname = input("Please input your Robot name:")
    robotname = 'robot519'
    leng = len(robotname)
    hostname = 'buildbox' + str(robotname[5:leng + 1]) + '.ariba.com'
    username = robotname
    password = robotname
    singleRobotOp = restartSingleRobot(hostname, username, password)
    robotcurrentstate = singleRobotOp.robotState()
    print("1111111:" + str(robotcurrentstate))
    if robotcurrentstate == 'running':
        print('execute 222222')
        num = input("Please select your Robot Operation:\n 1: Restart\n 2: Keep as it is\n:")
        if num == '1':
            print('\nYour Choice is "Restart"')
            # singleRobotOp.enableBash()
            time.sleep(2)
            singleRobotOp.stopService()
            robotcurrentstate1 = singleRobotOp.robotState()
            print("Your Robot's current state:")
            time.sleep(5)
            singleRobotOp.startService()
            time.sleep(5)
            robotcurrentstate2 = singleRobotOp.robotState()
            print(robotcurrentstate2)

        else:
            print('\nYour Choice is "Keep As It Is"')
    else:
        print('execute 333333333')
        print('The Robot state is STOPPED, Will restart Robot Service')

        # # singleRobotOp.enableBash()
        # time.sleep(2)
        # singleRobotOp.ssh.exec_command(
        #     "source /robots/machine-global-cache-for-personal-service/bashrc\n/robots/machine-global-cache-for-personal-service/usr/local/services/tools/bin/robot-control start")
        #
        # time.sleep(5)
        # robotcurrentstate3 = singleRobotOp.robotState()
        # print(robotcurrentstate3)
        singleRobotOp.ssh.exec_command(
            "source /robots/machine-global-cache-for-personal-service/bashrc")
        time.sleep(2)
        stdin, stdout, stderr = singleRobotOp.ssh.exec_command("/robots/machine-global-cache-for-personal-service/usr/local/services/tools/bin/robot-control start")

        lines = stderr.readlines()
        for line in lines:
            print(line)

    singleRobotOp.close()